#include "messagelistcontroller.h"

MessageListController::MessageListController(QObject *parent)
    : QObject{parent}
{
    messageList = new MessageList();
    setDriverIDNumber("0000");


     stringList = QStringList();
    for (int i = 0; i < 4; i++) {
         stringList.append("22:28 意外开门" + QString::number(i));
         stringList.append("22:28 超速" + QString::number(i));
         stringList.append("22:28 变速" + QString::number(i));
         stringList.append("22:28 驾驶模式无效驾驶" + QString::number(i));
    }

    messageList->listView->addItems(stringList);

    rowIndex = 4;
    connect(messageList->pushButton[0],SIGNAL(clicked()),this,SLOT(upScrollTo()));
    connect(messageList->pushButton[1],SIGNAL(clicked()),this,SLOT(downScrollTo()));
}

void MessageListController::show(QVBoxLayout *vLayout, int w)
{
    vLayout->addWidget(messageList,w);
}

void MessageListController::setDriverIDNumber(QString num)
{
    messageList->driverLabel[1]->setText(num);
}

void MessageListController::setAddItem(QString str)
{
    messageList->listView->addItem(str);
}

void MessageListController::upScrollTo()
{
    rowIndex = rowIndex > stringList.count()-5 ?stringList.count()-5:rowIndex;
    if(rowIndex > 0){
         rowIndex--;
    }

    QModelIndex indexFromList = messageList->listView->model()->index(rowIndex, 0);//取model
    messageList->listView->scrollTo(indexFromList);

}

void MessageListController::downScrollTo()
{
    rowIndex = rowIndex < 5 ?5:rowIndex;
    if(rowIndex < stringList.count()){
         rowIndex++;
    }
    QModelIndex indexFromList = messageList->listView->model()->index(rowIndex, 0);//取model
    messageList->listView->scrollTo(indexFromList);
}
